import {messageModel} from "../../mongodb/db";
import { Message } from "../../entities/content";

export class MessageServers {
    public static async add (message: {}) {
        //将平面对象转换为content实体类
        const newObj = Message.transformerThis(message);
        //验证
        const validateArr = await newObj.validatorClass();
        //添加
        if(validateArr.length !== 0) {
            return validateArr;
        }

        return await messageModel.create(message)
    }

    public static async find() {
        return await messageModel.find().sort({time: -1});
    }

    public static async delete (id: string) {
        return await messageModel.deleteOne({_id: id})
    }
}

